///<reference path="../crowd.service.ts"/>
import {
  Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, ViewChild, Input,
  OnDestroy
} from '@angular/core';
import {Router} from "@angular/router";
import {CrowdService} from "@crowd/crowd.service";
import {Subject} from "rxjs/Subject";

import {Subscription} from "rxjs/Subscription";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {CrowdBaseBeanH5VO} from "@interface/index";


@Component({
  selector: 'seed-crowd-list',
  templateUrl: './crowd-list.component.html',
  styleUrls: ['./crowd-list.component.scss', '../common.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class CrowdListComponent implements OnInit, OnDestroy {
  subscription: Subscription = new Subscription();
  public ngUnsubscribe: Subject<any> = new Subject<any>();
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public crowdList: CrowdBaseBeanH5VO[] = [];
  public checkData = true;
  public params = {
    page: 1,
    rows: 10
  };
  public stateStore = {
    haveData: true
  };
  constructor(
    public router: Router,
    public toolsService: ToolsService,
    public crowdService: CrowdService,
    private auth: AuthService,
  ) {
    this.getCrowdList();
  }

  ngOnInit() {

  }

  /**
  * @description 处理页面滚动加载
  * @author baoww-765
  * @date 2017-11-15
  * */
  getRecommendGoods() {
    setTimeout(() => {
      this.crowdService.getCrowdList(this.params)
        .takeUntil(this.ngUnsubscribe).debounceTime(800).subscribe(res => {
        this.infiniteScroll.complete();
        if (res.result === '0' && res.data.list && res.data.list.length > 0) {
          this.params.page++;
          this.crowdList = [...this.crowdList, ...res.data.list];
          if (this.crowdList.length >= 200 || !res.data.hasNextPage) {// 限制只加载200条
            this.infiniteScroll.enable(false);
          }
        }else {
          this.checkData = false;
        }
      });
    }, 800);
  }

  /**
  * @description 点击发起众筹，判断登录状态,判断当日是否还可以发起众筹，判断用户是否已经IP认证
  * @auth baoww-765
  * @date 2017-11-14
  * */
  lunchCrowd() {
    if (this.auth.isLoggedIn) {
      this.crowdService.getCanAdd().takeUntil(this.ngUnsubscribe).subscribe(res => {
        if (res.result === '0') {
          this.router.navigate([`/crowd/protocol`]);
        }else if (res.result === '50001') {
          this.toolsService.showToast('已达今日发起上限，请明日再来发起');
        }else if (res.result === '2110') {
          this.toolsService.showToast('想要发布众筹？请到求苗APP进行用户认证');
        }else {
          this.toolsService.showToast(res.msg);
        }
      });
    }else {
      const that = this;
      this.toolsService.presentConfirm('此操作需要登录，是否前往登录', 1, function (){
        that.router.navigate(['/auth/login']);
      });
    }

  }

  /**
  * @description 获取众筹列表页
  * @author baoww-765
  * @date 2017-11-12
  * */
  getCrowdList() {
    this.toolsService.showLoading();
    this.crowdService.getCrowdList(this.params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.params.page++;
        if (this.infiniteScroll) {
          this.infiniteScroll.complete();
        }
        // this.infiniteScroll && this.infiniteScroll.complete();
        this.crowdList = res.data.list;
        this.toolsService.hideLoading();
        if (!res.data.hasNextPage && this.infiniteScroll) {
           this.infiniteScroll.enable(false);
        }
        // !res.data.hasNextPage && (this.infiniteScroll && this.infiniteScroll.enable(false));
      }
    });
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
